/**
 * 
 */
package org.sword.lang;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author ChengNing
 * @date   2014年12月11日
 */
public class StreamUtils {
	
	/**
	 * stream to string
	 * @param is
	 * @return
	 */
	public static String streamToString(InputStream is){
		StringBuilder buffer = new StringBuilder();  
        BufferedReader reader=null;  
        try{  
            reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));  
            String line=null;  
            while((line = reader.readLine())!=null){  
                buffer.append(line);  
            }  
        }catch(Exception e){  
            e.printStackTrace();  
        }finally{  
            if(null!=reader){  
                try {  
                    reader.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return buffer.toString();
	}
	
	/**
	 * string to stream
	 * @param str
	 * @return
	 */
	public static InputStream strToStream(String str){
		InputStream is = new ByteArrayInputStream(str.getBytes());
		return is;
	}

}
